package com.bibrApp.difu.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import androidx.annotation.RequiresApi;
import okio.ByteString;

/**
 * Created by dj on 2019/9/10.
 */
public class NetWorkUtil {
  public static final String NETWORK_TYPE_WIFI = "WIFI";
  public static final String NETWORK_TYPE_2G = "2G";
  public static final String NETWORK_TYPE_3G = "3G";
  public static final String NETWORK_TYPE_LTE = "4G";

  private NetWorkUtil() {
    super();
  }

  /**
   * UnKnow chatSessionType
   */
  public static final String NETWORK_TYPE_UNKNOW = "unknow";

  /**
   * whether network connectivity exists and it is possible to establish
   *
   * @param context
   * @return state of NewWork
   */
  public static boolean isNetWorkConnected(Context context) {
    NetworkInfo networkInfo = getNetWorkInfo(context);
    return networkInfo != null && networkInfo.isConnected();
  }



  /**
   * 在子线程里开启该方法，可检测当前网络是否能打开网页
   * true是可以上网，false是不能上网
   */
  public static boolean isNetworkOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
      Process ipProcess = runtime.exec("ping -c 3 www.baidu.com");
      int exitValue = ipProcess.waitFor();
      return (exitValue == 0);
    } catch (IOException | InterruptedException e) {
      e.printStackTrace();
    }
    return false;
  }
  /**
   * 判断网络是否可用
   * 必须要在android6.0以上才可使用
   */
  @RequiresApi(api = Build.VERSION_CODES.M)
  public static boolean isNetworkAvailable(Context context) {
    try{
      ConnectivityManager connectivityManager =
              (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
      return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
    }catch (Exception e){
      e.printStackTrace();
    }
   return false;
  }

  /**
   * 判断是否为wifi连接
   *
   * @param context 上下文
   * @return
   */
  public static boolean isWifi(Context context) {
    return NETWORK_TYPE_WIFI.equals(getNetWorkType(context));
  }

  /**
   * @param context
   * @return NewWork chatSessionType
   */
  public static String getNetWorkType(Context context) {
    NetworkInfo netWorkInfo = getNetWorkInfo(context);
    if (netWorkInfo == null) {
      return NETWORK_TYPE_UNKNOW;
    }
    String type = NETWORK_TYPE_UNKNOW;
    switch (netWorkInfo.getType()) {
      case ConnectivityManager.TYPE_WIFI:
        type = NETWORK_TYPE_WIFI;
        break;
      case ConnectivityManager.TYPE_MOBILE:
        switch (netWorkInfo.getSubtype()) {
          case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
          case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
          case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
          case TelephonyManager.NETWORK_TYPE_1xRTT:
          case TelephonyManager.NETWORK_TYPE_IDEN:
            type = NETWORK_TYPE_2G;
            break;
          case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
          case TelephonyManager.NETWORK_TYPE_UMTS:
          case TelephonyManager.NETWORK_TYPE_EVDO_0:
          case TelephonyManager.NETWORK_TYPE_HSDPA:
          case TelephonyManager.NETWORK_TYPE_HSUPA:
          case TelephonyManager.NETWORK_TYPE_HSPA:
          case TelephonyManager.NETWORK_TYPE_EVDO_B:
          case TelephonyManager.NETWORK_TYPE_EHRPD:
          case TelephonyManager.NETWORK_TYPE_HSPAP:
            type = NETWORK_TYPE_3G;
            break;
          case TelephonyManager.NETWORK_TYPE_LTE:
            type = NETWORK_TYPE_LTE;
            break;
          default:
            break;
        }
        break;
      default:
        break;
    }
    return type;
  }

  /**
   * 打开网络设置界面
   */
  public static void openSetting(Activity activity) {
    Intent intent = new Intent("/");
    ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
    intent.setComponent(cm);
    intent.setAction("android.intent.action.VIEW");
    activity.startActivityForResult(intent, 0);
  }

  protected static NetworkInfo getNetWorkInfo(Context context) {
    try {
      ConnectivityManager connectivityManager =
          (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
      return networkInfo;
    } catch (Exception e) {
      return null;
    }
  }

  public static String md5Hex(String s) {
    try {
      MessageDigest messageDigest = MessageDigest.getInstance("MD5");
      byte[] md5bytes = messageDigest.digest(s.getBytes("UTF-8"));
      return ByteString.of(md5bytes).hex();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
      throw new AssertionError(e);
    }
  }
}
